package com.baidu.speech.qr_code.ui.dashboard;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.speech.qr_code.R;
import com.baidu.speech.qr_code.ui.Entity_class.Password;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class Dashboard_main extends AppCompatActivity {


    private TextView tv_content;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard_main);
        initviewa();
        initviewb();
    }

    private void initviewa() {
        // 创建IntentIntegrator对象
        IntentIntegrator intentIntegrator = new IntentIntegrator(Dashboard_main.this);
        //是否开启音效
        intentIntegrator.setBeepEnabled(true);
        //设置扫描页面下的文字提示
        intentIntegrator.setPrompt("请对准二维码！");
        // 开始扫描
        intentIntegrator.initiateScan();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 获取解析结果
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        String datata=result.getContents();
        //判断文本是否为链接
        if (Patterns.WEB_URL.matcher(datata).matches()) {
            Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
            //调用默认浏览器进入链接
            Intent intent= new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(datata);
            intent.setData(content_url);
            startActivity(intent);
            //获取实时时间
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date datetime=new Date(System.currentTimeMillis());
            Datazfh datazfh1 =new Datazfh();
            datazfh1.setTime(simpleDateFormat.format(datetime));
            datazfh1.setContent(datata);
            datazfh1.save();
            Toast.makeText(this, "储存成功"+datazfh1.getContent(), Toast.LENGTH_SHORT).show();
            tv_content.setText(datata);
        }else {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date datetime=new Date(System.currentTimeMillis());
            Datazfh datazfh1 =new Datazfh();
            datazfh1.setTime(simpleDateFormat.format(datetime));
            datazfh1.setContent(datata);
            datazfh1.save();
            Toast.makeText(this, "储存成功"+datazfh1.getContent(), Toast.LENGTH_SHORT).show();
            tv_content.setText(datata);

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initviewb() {
        tv_content = (TextView) findViewById(R.id.tv_content);
    }
}